// TODO: Make selecting folders possible.

var CollectionBrowser = new Class({
	initialize: function(startUrl, targetId) {
		return new Element('input', {
				'type': 'image',
				'src': sharpbar.mediaUrl + '/Resources/images/magnify.png',
				'alt': 'List relationship types',
                'value': 'Browse...',
                'id': 'location-browse',
                'events': {
                    'click': function(){
                        function showBrowser(results, url){
							if (results) {
								var browser = new sharpRegistry.widgets['modal'](url, results, new Hash({
									'containerId': 'browse-folder-popover',
									'windowTitleBarId': 'browse-window-title',
									'paneConfig': new Hash({			
										'scroll': true,
										'height': '200px',
										'class': sharpRegistry.panes["default.folder"],
										'displayElementId': 'browse-folder-listing-div', 
										'parentElementId': 'browse-folder-popover', 
										'trackPane': false,
										'embedListInDiv': false, 
										'listingDivId': 'browse-folder-item-listing-div', 
										'listingListId': 'browse-folder-item-listing',
										'clickCallback': function(pane, filePath, urlSegment){
											if (urlSegment) {
												var lastChar = filePath.charAt(filePath.length - 1);
												url = lastChar == "/" ? filePath : lastChar == "#" ? filePath.slice(filePath.length - 1) + "/" : filePath + "/";
												url = url + urlSegment.slice(urlSegment.lastIndexOf('/') + 1);
											}
											else {
												url = filePath.slice(0, filePath.slice(0, filePath.length).lastIndexOf('/'));
											}
											
											mc.propfind(url, function(results){
												var items = results;
												if (items) {
													if ((itemList = items.getElementsByTagName('D:response')) && itemList.length > 1) {
														pane.getPane(url, items);
														if (pane.navigationPane) 
															pane.show();
													}
													else {
														var item = itemList[0];
														var href = item.getElementsByTagName('D:href')[0];
														if (href.firstChild && href.firstChild.nodeValue) {
															var target = document.id(targetId);
															if (target) {
																target.focus();
																target.value = sharpbar.applicationUrl + href.firstChild.nodeValue;
																pane.hide();
															}
														}
													}
												}
											}, null, ['D:displayname', 'mc:mimetype', 'mc:resourceprovider', 'mc:version', 'mc:published', 'mc:href'])
										}
									})
								}));
								if (browser) {
									browser.show();
									window.scrollTo(0,0);
								}
							}
                        }
                        
                        var results = mc.propfind(startUrl, function(results){
							showBrowser(results, startUrl)
						}, false, ['D:displayname', 'mc:mimetype', 'mc:resourceprovider', 'mc:version', 'mc:published', 'mc:href']);
                    }.bind(this)
                }
            })
	}
});
